package com.cdqidi.core.weixinmanager.service.wx;

import com.jfinal.weixin.sdk.api.ApiResult;
import com.jfinal.wxaapp.WxaConfigKit;
import com.jfinal.wxaapp.api.WxaAccessTokenApi;
import com.jfinal.wxaapp.api.WxaMessageApi;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

/**
 * 〈一句话功能简述〉<br>
 *
 * @author sjk
 * @since 1.0.0
 */
@Service
@Slf4j
public class WxaMsgService {

    @Async
    public void sendWxaMessage(String appId, String openId, String text, String mediaId) {
        try {
            WxaConfigKit.setThreadLocalAppId(appId);
            ApiResult result = WxaMessageApi.sendText(openId, text);
            if (!result.isSucceed()) {
                log.info("发送文本: " + result.getJson() + ",openId: " + openId + ",appId: " + WxaConfigKit.getAppId() + ",token: " + WxaAccessTokenApi.getAccessTokenStr());
            }
            result = WxaMessageApi.sendImage(openId, mediaId);
            if (!result.isSucceed()) {
                log.info("发送图片: " + result.getJson() + ",openId: " + openId + ",appId: " + WxaConfigKit.getAppId() + ",token: " + WxaAccessTokenApi.getAccessTokenStr());
            }
        } finally {
            WxaConfigKit.removeThreadLocalAppId();
        }
    }
}